/*
 * WriteMemo.java
 *
 * Created on 2008年5月20日, 下午5:44
 */
package calendar;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

import javax.swing.JOptionPane;

/**
 *
 * @author  啊正
 */
public class WriteMemo extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	//备忘录的日期
    private String date;
    //保存备忘录的路径
    private static final String path = "D:/Calendar_Memo/Memo.BOX";
    private File file = null;

    public WriteMemo(String date) {
        initComponents();
        this.date = date;
        this.setTitle("编写备忘录  " + date);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPane = new javax.swing.JPanel();
        spTa = new javax.swing.JScrollPane();
        taMsg = new javax.swing.JTextArea();
        btnSave = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("编写备忘录");
        setModal(true);
        setResizable(false);

        contentPane.setBackground(new java.awt.Color(204, 204, 204));

        spTa.setBackground(new java.awt.Color(255, 255, 255));
        spTa.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 255), 1, true), "备忘录内容"));

        taMsg.setColumns(20);
        taMsg.setLineWrap(true);
        taMsg.setRows(5);
        spTa.setViewportView(taMsg);

        btnSave.setText("保存");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnReset.setText("重写");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout contentPaneLayout = new javax.swing.GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                .addContainerGap(238, Short.MAX_VALUE)
                .addComponent(btnReset)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSave)
                .addContainerGap())
            .addComponent(spTa, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(spTa, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(contentPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnReset))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contentPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contentPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-376)/2, (screenSize.height-241)/2, 376, 241);
    }// </editor-fold>//GEN-END:initComponents
    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        taMsg.setText("");
        taMsg.grabFocus();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        saveMemo();
    }//GEN-LAST:event_btnSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JPanel contentPane;
    private javax.swing.JScrollPane spTa;
    private javax.swing.JTextArea taMsg;
    // End of variables declaration//GEN-END:variables
    private void saveMemo() {
        //获取文本区域中的内容
        String memo = taMsg.getText().trim();
        //创建一个用于保存内容的字符串
        String writeFile = new String();
        //创建一个用于读取原文件内容的字符串
        String readFile = new String();
        //若本次编写备忘录内容为空,则提示用户
        if (memo.length() == 0) {
            JOptionPane.showMessageDialog(this, "备忘录内容不能为空");
            return;
        }
        //文件不存在时需要自动创建
        if (file == null) {
            file = new File(path);
        }
        if (!file.exists()) {
            try {
                //创建文件必须的文件夹
                file.getParentFile().mkdirs();
                //创建相应文件
                file.createNewFile();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "创建备忘录文件失败!");
                return;
            }
        }
        try {
            //从字符输入流中读取文本，缓冲各个字符，从而实现字符、数组和行的高效读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while ((readFile = bf.readLine()) != null) {    //读取一个文本行
                //将读取到的文本行保存下来并在其后面加上回车(即同时换行)
                writeFile += readFile + "\r\n";
            }
            //关闭字符输入流
            bf.close();
            //在原文件内容中加入本次的备忘录内容(注意必须加日期)
            writeFile += date + "," + memo;
            //将文本写入字符输出流，缓冲各个字符，从而提供单个字符、数组和字符串的高效写入
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            //写入字符串
            bw.write(writeFile);
            //关闭字符输出流
            bw.close();
            JOptionPane.showMessageDialog(this, "已保存");
        } catch (Exception e) {
            System.out.println("------" + e.getMessage() + "------");
        }
    }
}
